package srjava.presenter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;

import javax.swing.*;

import org.apache.xmlrpc.XmlRpcException;

import srjava.messages.*;
import srjava.messages.ServerFullInfo;
import srjava.server.*;

public class Presenter extends JFrame implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private RemoteCoordinator remoteCoordinator;
	private RemoteServer remoteServer;
	private JTextField ip;
	private JTextField port;
	private JButton start;
	private JTable table;
	private ServerTableModel tableModel;
	private boolean interrupted = true;;
	
	public Presenter() {
		setTitle("Presenter SR - sewery rozproszone");
		ip = new JTextField("127.0.0.1");
		port = new JTextField("8000");
		start = new JButton("Start");
		table = new JTable(8, 3);
		tableModel = new ServerTableModel();
		table.setModel(tableModel);
		this.setSize(500, 250);
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.7;
		c.gridx = 0;
		c.gridy = 0;
		panel.add(ip, c);
		c.weightx = 0.3;
		c.gridx = 1;
		panel.add(port, c);
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 1;
		start.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (start.getText().equals("Start")) {
					interrupted = false;
					start.setText("Stop");
				} else {
					start.setText("Start");
					interrupted = true;
				}
			}
		});
		panel.add(start, c);
		c.gridx = 0;
		c.gridy = 2;
		panel.add(table, c);
		panel.setVisible(true);
		add(panel);
		setVisible(true);
	}
	
	public static void main(String args[]) {
		Presenter presenter = new Presenter();
		presenter.run();
	}

	@Override
	public void run() {
		while(true)	{
			try {
				Thread.sleep(1250);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (!interrupted) {
				askForCoordinator();
				displayNew();
			}
		}		
	}

	private void displayNew() {
		if (remoteCoordinator == null) return;
		NumberOfTasksMsg msg = new NumberOfTasksMsg();
		try {
			String s = remoteCoordinator.sendMessage(msg.serialize());
			StringReader sr = new StringReader(s);
			IntString.parseInt(sr); // to jest wazne!
			NumberOfTasksRet msgRet = new NumberOfTasksRet();
			msgRet.deSerialize(sr);
			tableModel.setTableList(msgRet.getServerList());
		} catch (XmlRpcException e) {
			System.out.println("Problem polaczenia z koordynatorem!");
		} catch (IOException e) {
			System.out.println("Prosze pani! A koordynator zle odpowiada!");
		}
	}

	private void askForCoordinator() {
		if (remoteCoordinator != null) return;
		if (remoteServer == null) {
			remoteServer = new RemoteServer(new ServerInfo(
				new Address(new SrString(ip.getText()), Integer.parseInt(port.getText())),
				new SrString("")));
		}
		CordinatorAddresMsg msg = new CordinatorAddresMsg();
		try {
			String s = remoteServer.sendMessage(msg.serialize());
			StringReader sr = new StringReader(s);
			IntString.parseInt(sr);
			CordinatorAddresRet retMsg = new CordinatorAddresRet();
			retMsg.deSerialize(sr);
			remoteCoordinator = new RemoteCoordinator();
			remoteCoordinator.setCoordinator(retMsg.getCordinatorInfo());
		} catch (XmlRpcException e) {
			System.out.println("Problem polaczenia z serwerem!");
		} catch (IOException e) {
			System.out.println("Prosze pani! A serwer zle odpowiada!");
		}
	}

}
